<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

class ImageProcessor {


    // -------------------------------------------------------- Public Methods


    /**
     * Process 1 image uploaded by a file input field.
     *
     * @param conn The Propel database connection.
     * @param errors The MVC ActionErrors.
     * @param formImage The uploaded php image array.
     * @param properties The resize properties.
     * @param image The Image Propel Object.
     * @param thumb The Image Propel Object.
     * @param galleryImage The GalleryImage Propel Object.
     */
    public function process(Connection $conn, ActionMessages &$errors, $formImage,
            ImageProperties $properties, Image $image, Image $thumb) {

        if(!is_uploaded_file($formImage["tmp_name"])) {
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("No image was uploaded."));
            throw new Exception("No image was uploaded.");
        }

        // Create temporary filenames.
        $temp = $formImage["tmp_name"].".temp";

        // Get the original image dimensions.
        $size = getimagesize($formImage["tmp_name"]);

        // Image resizing.
        if(($properties->getImageAxis() == "x" && $size[0] > $properties->getImageWidth()) ||
           ($properties->getImageAxis() == "y" && $size[1] > $properties->getImageHeight()) ||
            $properties->getImageMode() == 2) {

            require_once ("cms/core/util/Image_Toolbox.class.php");
            $img = new Image_Toolbox($formImage["tmp_name"]);
            $img->setResizeMethod("resample");

            $img->newOutputSize($properties->getImageWidth(),
                $properties->getImageHeight(), $properties->getImageMode(),
                false, "#".$properties->getImageColor());
            $img->save($temp);

            $image->saveFile($temp, $formImage["name"], $conn);

            // Delete the tmp file
            if(file_exists($temp)) unlink($temp);

        } else {

            $image->saveForm($formImage, $conn);

        }


        // Thumbnail resizing.
        if(($properties->getThumbAxis() == "x" && $size[0] > $properties->getThumbWidth()) ||
           ($properties->getThumbAxis() == "y" && $size[1] > $properties->getThumbHeight())) {

            require_once ("cms/core/util/Image_Toolbox.class.php");
            $img = new Image_Toolbox($formImage["tmp_name"]);
            $img->setResizeMethod("resample");

            $img->newOutputSize($properties->getThumbWidth(),
                $properties->getThumbHeight(), $properties->getThumbMode(),
                false, "#".$properties->getThumbColor());
            $img->save($temp);

            $thumb->saveFile($temp, $formImage["name"], $conn);

            // Delete the tmp file
            if(file_exists($temp)) unlink($temp);

        } else {

            $thumb->saveForm($formImage, $conn);

        }

        return array("thumb"=>$thumb->getImageId(), "image"=>$image->getImageId());

    }

}
?>